package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduStudentGradeDTO;
import com.zenith.edu.po.vo.EduStudentGradeVO;
import com.zenith.edu.service.EduStudentGradeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/student-grade")
@Tag(name = "学生成绩管理", description = "学生成绩管理接口")
public class EduStudentGradeController {
    
    @Autowired
    private EduStudentGradeService eduStudentGradeService;
    
    @SecurePostMapping(value = "/createStudentGrade", roles = "ROLE_ADMIN", permission = "student-grade:create")
    @Operation(summary = "创建学生成绩")
    public Result<EduStudentGradeVO> createStudentGrade(@RequestBody EduStudentGradeDTO dto) {
        EduStudentGradeVO studentGradeVO = eduStudentGradeService.createStudentGrade(dto);
        return Result.success(studentGradeVO);
    }
    
    @SecurePostMapping(value = "/getStudentGradeById/{id}", roles = "ROLE_ADMIN", permission = "student-grade:read")
    @Operation(summary = "根据ID获取学生成绩详情")
    public Result<EduStudentGradeVO> getStudentGradeById(@PathVariable Long id) {
        EduStudentGradeVO studentGradeVO = eduStudentGradeService.getStudentGradeById(id);
        if (studentGradeVO == null) {
            return Result.fail("学生成绩不存在");
        }
        return Result.success(studentGradeVO);
    }
    
    @SecurePostMapping(value = "/updateStudentGrade", roles = "ROLE_ADMIN", permission = "student-grade:update")
    @Operation(summary = "更新学生成绩信息")
    public Result<EduStudentGradeVO> updateStudentGrade(@RequestBody EduStudentGradeDTO dto) {
        EduStudentGradeVO studentGradeVO = eduStudentGradeService.updateStudentGrade(dto);
        return Result.success(studentGradeVO);
    }
    
    @SecurePostMapping(value = "/deleteStudentGrade/{id}", roles = "ROLE_ADMIN", permission = "student-grade:delete")
    @Operation(summary = "删除指定学生成绩")
    public Result<Void> deleteStudentGrade(@PathVariable Long id) {
        boolean result = eduStudentGradeService.deleteStudentGrade(id);
        if (!result) {
            return Result.fail("学生成绩删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getStudentGradeList", roles = "ROLE_ADMIN", permission = "student-grade:read")
    @Operation(summary = "获取学生成绩列表")
    public Result<List<EduStudentGradeVO>> getStudentGradeList() {
        List<EduStudentGradeVO> studentGradeList = eduStudentGradeService.getStudentGradeList();
        return Result.success(studentGradeList);
    }
}